package com.cskaoyan.market.config;

import com.cskaoyan.market.filter.CharacterEncodingFilter;
import com.cskaoyan.market.filter.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

//此处在SpringBoot中和在SpringMVC中略有不同，不可以写@EnableWEBMvc注解，在springboot中会对其他注解产生影响
@Configuration
public class WebConfig implements WebMvcConfigurer {

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/static/**").addResourceLocations("file:D:/upload/");
//    }

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> corsFilter(){
        FilterRegistrationBean<OncePerRequestFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CorsFilter());
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }


    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> myCharacterEncodingFilter(){
        FilterRegistrationBean<OncePerRequestFilter> bean2 = new FilterRegistrationBean<>();
        bean2.setFilter(new CharacterEncodingFilter());
        bean2.setUrlPatterns(Arrays.asList("/admin/*"));
        return bean2;
    }


}
